Repository navigation
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
This was referenced Nov 9, 2026
This was referenced Nov 9, 2026
This was referenced Nov 9, 2026
3outeille
force-pushed
the
cp-1-implementations
branch
from
November 10, 2026 02:50
ba95ec6 to
a36d4f7
Compare
3outeille
force-pushed
the
cp-1-implementations
branch
2 times, most recently
from
November 10, 2026 05:44
d0e004f to
16affa7
Compare
3outeille
force-pushed
the
cp-1-implementations
branch
3 times, most recently
from
November 12, 2026 08:01
6b06ac3 to
4d31656
Compare
distributed] Context parallel implementations, with Ulyssesdistributed] Context parallel module, with Ulysses
3outeille
force-pushed
the
cp-1-implementations
branch
from
November 12, 2026 09:13
4d31656 to
6e942d0
Compare
Each rank of a `cp` device mesh holds a contiguous slice of every sequence. The plan pairs two kinds of styles: `cp_split_sequence` cuts the layer inputs to that slice, and an attention style, `cp_ulysses`, runs the attention call of the attention modules over the whole sequence, with the mask and the keyword arguments the model built before the sequence was cut. Ulysses is two all-to-all exchanges around the model's own attention function; another algorithm is a `ContextParallelAttention` subclass registered in `ALL_PARALLEL_STYLES`. The styles share `ContextParallelLayer`, with the `install_forward` contract of the TP and EP styles. `apply_context_parallelism` installs a resolved plan on a model, and `ModelContextParallel` on the model itself: the model checks, a forward without a cache, and the loss of the whole sequences, which every rank reports. Not wired into `DistributedConfig` yet: nothing changes for users.
The all-to-all and its backward, Ulysses against plain sdpa on the whole sequence given the whole mask (unpacked, packed, sliding window, with and without GQA), the split style on a stand-in layer, the routing of the attention call, the Ulysses and model checks, the loss against the full-sequence loss, and a tiny Llama with the plan installed by hand against the same weights on the full sequences. The multi-rank tests use two CPU ranks through `run_distributed`, and carry `is_fsdp_test` so that the sharded CI jobs own them.
3outeille
force-pushed
the
cp-1-implementations
branch
from
November 12, 2026 09:29
6e942d0 to
318ef65
Compare
The context parallel styles live in their own registry, `ALL_CP_STYLES`, like `ALL_FSDP_STYLES`, instead of `ALL_PARALLEL_STYLES` plus a filter on their class. `apply_context_parallelism` reads it, and another attention algorithm is added to it under its own name. The layer types that `ModelContextParallel.check` accepts are inlined in the check.
…ks in `apply_context_parallelism`, pad the sequences - `context_parallel_loss` is a module-level function that `apply_context_parallelism` sets as `model.loss_function`, the seam transformers has for a model's loss; `ModelContextParallel` is about the forward only. - The model checks open `apply_context_parallelism` instead of a `check` method. - A sequence length that `cp_size` does not divide is padded at its end to a multiple of it, in `ModelContextParallel.transform_inputs_pre_forward`, before the model builds its masks: `input_ids` get `pad_token_id` (or 0), `inputs_embeds` zeros, `attention_mask` zeros, `labels` and `shift_labels` -100, and `position_ids` continue those of the last document. The logits are this rank's slice of the padded sequence; the loss counts the real tokens only. Was #50515.
…he two transforms of a style `ContextParallelLayer` gains `transform_output_post_forward`, so a style can rewrite the output of the forward it wraps, as the tensor parallel styles do. `ModelContextParallel` uses both: its inputs lose the cache, are padded to a multiple of `cp_size`, and carry this rank's slice of the shifted labels with the token count of the whole sequences, which the model's own loss function takes as kwargs; its output reports the loss of the whole sequences on every rank, with the gradient of this rank's slice scaled by `cp_size` for FSDP2's average. `context_parallel_loss` and the replacement of `model.loss_function` are gone.
`ALL_CP_STYLES` lists everything context parallelism installs, the model's own style included, and `apply_context_parallelism` installs the root through the same loop as the plan, under the name `named_modules` gives it.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Stack: this PR → #50496 → #50511 → #50514 → #50499 → #50521 → #50522
Adds
transformers/distributed/context_parallel.py, the whole context parallel (CP) module. Nothing is wired yet: noDistributedConfigfield, no plan on the configs, nocpmesh axis. #50496 brings those.What this PR brings
ContextParallelLayer, the base of the styles below, with theinstall_forwardcontract of the TP and EP styles: it replacesmodule.forwardby one that runstransform_inputs_pre_forwardfirst (withfunctools.wraps, soinspect.signature(model.forward)stays whatTrainerexpects)."cp_split_sequence"(SplitSequenceParallel), for decoder layers: they run on this rank's contiguous slice of the sequence. The first layer of the plan cutshidden_states, and every layer cuts theposition_embeddings. The attention mask andposition_idsstay those the model built for the whole sequence.ContextParallelAttention, the base of the attention styles:install_forwardchecks the module, gives itattentionas itscp_attention, and wraps the model's entry inALL_ATTENTION_FUNCTIONSso that the attention call of that module runs through it. The algorithm is a plan style, like a TP style:"cp_ulysses"(UlyssesAttention) all-to-alls the query, key and value to the whole sequence withnum_heads / cp_sizeheads, runs the model's own attention function with the model's own mask, and all-to-alls the output back. Sliding windows, chunked attention, padding and packing therefore need no CP mask code. Another algorithm is a subclass registered inALL_PARALLEL_STYLESand named in the plan;context_parallel_styles()lists them for the plan validation.ModelContextParallel, the style of the model itself, outside the plan since no plan key names the root module: its forward refuses a cache, and pads every sequence at its end to a multiple ofcp_sizebefore the model builds its masks (input_idsgetpad_token_idor 0,inputs_embedsandattention_maskzeros,labelsandshift_labels-100,position_idscontinue the last document). The logits are this rank's slice of the padded sequence.context_parallel_loss, set asmodel.loss_function: every rank reports the loss of the whole sequences, computed from its slice of the logits and its slice of the shifted labels, with a gradient of this rank's timescp_size, so that FSDP2's average over the cp ranks is the gradient of the full-sequence loss.apply_context_parallelism(model, cp_mesh, cp_plan): refuses what CP would silently get wrong (attention that does not go through the attention interface, layer types other than softmax attention, heads other than a causal LM), then installsModelContextParalleland the loss on the model and the plan's styles on their modules.all_to_allandsplit_sequence, the two helpers the above use. The all-to-all is torch'sall_to_all_single_autograd, which EP uses too.What #50496 brings
DistributedConfig(cp_size, cp_plan),base_model_cp_planon the configs and the mixin's plan resolution, thecpmesh axis with FSDP2 overfsdp × cp, the models that opt out, theTrainerrefusal, and the end-to-end tests throughfrom_pretrained(distributed_config=...). It adds no code tocontext_parallel.py.Tests
tests/context_parallel/test_context_parallel.py, on 2 CPU ranks (gloo):sdpaon the full sequence with the full mask (causal, packed, sliding window, GQA), forward and backward;num_items_in_batch, a slice whose shifted labels are all ignored;model.forwardkeeps its signature;